<template>
  <div class="account-container">
    <h1 class="account-title">我的账户</h1>
    <div class="cont" v-if="cartData.length">
      <el-card class="box-card" v-for="(item ,index) in cartData" :key="index">
        <div slot="header" class="clearfix">
          <span>卡号：{{ item.cardID }}</span>
          <el-button style="float: right; padding: 3px 0" type="text">
            <span><el-button type="success" size="mini" @click="edit()">修改密码</el-button></span>
            <span><el-button type="danger" size="mini" @click="Delete(item.cardID)">删除</el-button></span>
          </el-button>
        </div>
        <el-dialog title="修改密码" :visible.sync="dialogFormVisible">
          <el-form :model="form">
            <el-form-item label="原密码" :label-width="formLabelWidth" required>
              <el-input v-model="form.pass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="新密码" :label-width="formLabelWidth" required>
              <el-input v-model="form.newpass" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="submit(item)">确 定</el-button>
          </div>
        </el-dialog>
        <div class="text item" ref="pass">
          交易密码 ：
          <!--          <el-tag v-show="show2" disable-transitions>{{ item.pass }}</el-tag>-->
          <el-tag v-show="!show2" disable-transitions>--------------------------</el-tag>
        </div>
        <div class="text item">
          <p v-if="item.cardType==='储蓄卡'">卡类型：
            <el-tag color="#152d64">{{ item.cardType }}</el-tag>
          </p>
          <p v-else>卡类型：
            <el-tag color="#aff0af">{{ item.cardType }}</el-tag>
          </p>
        </div>
        <div class="text item">
          <p>开卡时间：{{ item.openDate }}</p>

        </div>
        <div class="text item">
          <p>余额：
            <el-tag type="danger" color="#05d1f2" size="medium" style="font-size: 20px">{{ item.balance }} $</el-tag>
          </p>
        </div>
      </el-card>
    </div>
    <el-empty :image-size="300" description="暂无数据，你还没有办理卡" v-else>
      <el-button type="primary" @click="$router.push('AddCard')">立即办理</el-button>
    </el-empty>
  </div>
</template>

<script>


export default {
  data() {
    return {
      show: true,
      show2: false,
      cartData: "",
      user: JSON.parse(window.localStorage.getItem("user")),
      form: {
        pass: "",
        newpass: "",
      },
      dialogFormVisible: false,
      formLabelWidth: '120px'
    }
  },
  mounted() {
    this.initDate();
  },
  methods: {
    initDate() {
      let id = this.user[0].customerID
      this.getRequest(`/api/card/user/${id}`).then(resp => {
        if (resp) {
          if (resp.code == 1) {
            this.cartData = resp.data;
            console.log(this.cartData)
          }
        }
      })
    },
    Delete(row) {
      this.deleteRequest(`/api/card/${row}`).then(resp => {
        if (resp) {
          if (resp.code == 1) {
            this.$message.success("删除成功")
            this.initDate();
          }
        }
      })
    },
    edit() {
      this.dialogFormVisible = true
    },
    submit(item) {
      if (item.pass === this.form.pass) {
        const data = {
          cardID: item.cardID,
          pass: this.form.newpass
        }
        this.putRequest("/api/card", data).then(resp => {
          if (resp) {
            if (resp.code == 1) {
              this.$message.success("修改成功")
              this.dialogFormVisible=false;
            }
          }
        })
      } else {
        this.$message.error("原密码错误")
      }
    }
  }
}
</script>

<style scoped>

.account-title {
  text-align: center;
  font-size: 24px;
  margin-bottom: 20px;
}

.text {
  font-size: 18px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}

.box-card {
  width: 480px;
  text-align: left;
  margin-top: 10px;
}

.cont {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
  align-items: center;
}
</style>
